package leetcode.editor.cn;

import java.util.Arrays;

/**
 * @author: yangwk
 * @date: 2023/10/18
 * @description:
 **/
public class P478 {


    /**
     * Given the radius and the position of the center of a circle, implement the function randPoint which generates a uniform random point inside the circle.
     * <p>
     * Implement the Solution class:
     * <p>
     * Solution(double radius, double x_center, double y_center) initializes the object with the radius of the circle radius and the position of the center (x_center, y_center).
     * randPoint() returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array [x, y].
     */
    double x = 5;
    double y = -7.5;
    double radius = 10;

    public static void main(String[] args) {
        Arrays.stream(new P478().randPoint()).forEach(System.out::println);
//        System.out.println(new P478().inArea(-8.48072,2.85149));

    }

    public boolean inArea(double xPoint, double yPoint) {
        double v = Math.pow(Math.abs(x - xPoint), 2) + Math.pow(Math.abs(y - yPoint), 2);
        return v <= Math.pow(radius, 2);
    }

    public double[] randPoint() {
        while (true) {
            double rand1 = rand();
            double rand2 = rand();
            if (Math.pow(rand1, 2) + Math.pow(rand2, 2) <= Math.pow(radius, 2)) {
                double random1 = Math.random();
                double random2 = Math.random();
                if (random1 >= 0.5) {
                    rand1 = -rand1;
                }
                if (random2 >= 0.5) {
                    rand2 = -rand2;
                }
                return new double[]{rand1 + x, rand2 + y};
            }
        }
    }

    public double rand() {
        double randX = Math.random() * this.radius;
        if (randX == 0) {
            double random = Math.random();
            if (random >= 0.5) {
                randX = radius;
            }
        }
        return randX;
    }

}
